Public Class formidentification

    ' Tells if the application can be exited.
    Private m_bAllowClose As Boolean = False

    ' The vault is stored here.
    Dim m_oVault As MFilesAPI.Vault = Nothing

    ' The selected vault name, given during initialization.
    Dim m_szVaultName As String = ""

    ' Initialize the dialog.
    Public Sub Initialize(ByVal szVaultName As String)
        m_szVaultName = szVaultName
    End Sub

    ' Gets the vault.
    Public Function GetVault() As MFilesAPI.Vault
        Return m_oVault
    End Function

    ' Fires when the form is loading.
    Private Sub formidentification_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    ' Fires when the form is closing.
    Private Sub formidentification_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing

        ' Check if the closing is allowed.
        If Not m_bAllowClose Then

            ' Cancel the close operation.
            e.Cancel = True

        End If

    End Sub

    ' Anonymous button clicked.
    Private Sub btnAnonymous_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAnonymous.Click

        ' Set wait cursor on.
        Me.Cursor = Cursors.WaitCursor

        Try

            ' Get vault connection.
            Dim oMFApp As New MFilesAPI.MFilesClientApplication
            Dim oVC As MFilesAPI.VaultConnection = oMFApp.GetVaultConnection(m_szVaultName)

            ' Log out.
            If (oVC.IsLoggedIn = True) Then
                oVC.BindToVault(Me.Handle, False, False).LogOutWithDialogs(Me.Handle)
            End If

            ' Reset vault login information.
            oMFApp.RemoveVaultConnection(m_szVaultName, oVC.UserSpecific)
            oVC.Domain = ""
            oVC.Password = ""
            oVC.UserName = ""
            oVC.AuthType = MFilesAPI.MFAuthType.MFAuthTypeLoggedOnWindowsUser
            oVC.AutoLogin = False
            oMFApp.AddVaultConnection(oVC)

            ' Bind to vault.
            m_oVault = oVC.BindToVault(Me.Handle, True, False)

            ' Close the dialog.
            m_bAllowClose = True
            Me.Close()

        Catch ex As Exception

            ' Show possible error message.
            MsgBox(ex.Message, MsgBoxStyle.Information)

        End Try

        ' Set wait cursor off.
        Me.Cursor = Cursors.Default

    End Sub

    ' Log In button clicked.
    Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogin.Click

        ' Set wait cursor on.
        Me.Cursor = Cursors.WaitCursor

        Try

            ' Get vault connection.
            Dim oMFApp As New MFilesAPI.MFilesClientApplication
            Dim oVC As MFilesAPI.VaultConnection = oMFApp.GetVaultConnection(m_szVaultName)

            ' Log out.
            If (oVC.IsLoggedIn = True) Then
                oVC.BindToVault(Me.Handle, False, False).LogOutWithDialogs(Me.Handle)
            End If

            ' Reset vault login information.
            oMFApp.RemoveVaultConnection(m_szVaultName, oVC.UserSpecific)
            oVC.Domain = ""
            oVC.Password = ""
            oVC.UserName = ""
            oVC.AutoLogin = False
            oVC.AuthType = MFilesAPI.MFAuthType.MFAuthTypeSpecificWindowsUser
            oMFApp.AddVaultConnection(oVC)

            ' Bind to vault. Returns NULL if the user cancel the operation.
            m_oVault = oVC.BindToVault(Me.Handle, True, True)

            ' Close the dialog if not cancelled.
            If (IsNothing(m_oVault) = False) Then
                m_bAllowClose = True
                Me.Close()
            End If

        Catch ex As Exception

            ' Show possible error message.
            MsgBox(ex.Message, MsgBoxStyle.Information)

        End Try

        ' Set wait cursor off.
        Me.Cursor = Cursors.Default

    End Sub

    ' Back button clicked.
    Private Sub btnBack_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBack.Click

        ' Reset the vault.
        m_oVault = Nothing

        ' Close the dialog.
        m_bAllowClose = True
        Me.Close()

    End Sub

End Class